set(CMAKE_MODULE_PATH "@MODULES_DIR@")
set(CMAKE_CURRENT_SOURCE_DIR "@CMAKE_CURRENT_SOURCE_DIR@")
set(CMAKE_CURRENT_BINARY_DIR "@CMAKE_CURRENT_BINARY_DIR@")

include(ECMGenerateHeaders)
include(CMakeParseArguments)

function (check_files)
    set(options)
    set(oneValueArgs GENERATED ORIGINALS)
    set(multiValueArgs)
    cmake_parse_arguments(CF "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
    list(LENGTH CF_GENERATED count)
    math(EXPR last "${count}-1")
    foreach(i RANGE ${last})
        list(GET CF_GENERATED ${i} generated_file)
        list(GET CF_ORIGINALS ${i} original_file)
        if (NOT EXISTS "${generated_file}")
            message(FATAL_ERROR "${generated_file} was not generated")
        endif()
        file(READ "${generated_file}" file_contents)
        string(STRIP "${file_contents}" file_contents)
        set (exp_contents "#include \"${original_file}\"")
        if (NOT "${file_contents}" STREQUAL "${exp_contents}")
            message(FATAL_ERROR "${generated_file} contains '${file_contents}' instead of '${exp_contents}'")
        endif()
    endforeach()
endfunction()

###########################################################

message(STATUS "Test 1: no optional arguments")
set(camelcase_headers)
set(expfiles "${CMAKE_CURRENT_BINARY_DIR}/HeadTest1"
             "${CMAKE_CURRENT_BINARY_DIR}/HeadTest2")
set(origfiles headtest1.h headtest2.h)
file(REMOVE ${expfiles})
ecm_generate_headers(
    camelcase_headers
    HEADER_NAMES HeadTest1 HeadTest2
)
if (NOT "${expfiles}" STREQUAL "${camelcase_headers}")
    message(FATAL_ERROR "camelcase_headers was set to \"${camelcase_headers}\" instead of \"${expfiles}\"")
endif()
check_files(GENERATED ${expfiles}
            ORIGINALS ${origfiles})


###########################################################

message(STATUS "Test 2: RELATIVE")
set(camelcase_headers)
set(expfiles "${CMAKE_CURRENT_BINARY_DIR}/HeadTest2"
             "${CMAKE_CURRENT_BINARY_DIR}/HeadTest3")
set(origfiles headtest2.h headtest3.h)
file(REMOVE ${expfiles})
ecm_generate_headers(
    camelcase_headers
    HEADER_NAMES HeadTest2 HeadTest3
    RELATIVE headsubdir
)
if (NOT "${expfiles}" STREQUAL "${camelcase_headers}")
    message(FATAL_ERROR "camelcase_headers was set to \"${camelcase_headers}\" instead of \"${expfiles}\"")
endif()
check_files(GENERATED ${expfiles}
            ORIGINALS ${origfiles})


###########################################################

message(STATUS "Test 3: OUTPUT_DIR")
set(camelcase_headers)
set(expfiles "${CMAKE_CURRENT_BINARY_DIR}/testdir/HeadTest1"
             "${CMAKE_CURRENT_BINARY_DIR}/testdir/HeadTest2")
set(origfiles headtest1.h headtest2.h)
file(REMOVE ${expfiles})
ecm_generate_headers(
    camelcase_headers
    HEADER_NAMES HeadTest1 HeadTest2
    OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/testdir"
)
if (NOT "${expfiles}" STREQUAL "${camelcase_headers}")
    message(FATAL_ERROR "camelcase_headers was set to \"${camelcase_headers}\" instead of \"${expfiles}\"")
endif()
check_files(GENERATED ${expfiles}
            ORIGINALS ${origfiles})


###########################################################

message(STATUS "Test 4: PREFIX")
set(camelcase_headers)
set(expfiles "${CMAKE_CURRENT_BINARY_DIR}/Module/HeadTest1"
             "${CMAKE_CURRENT_BINARY_DIR}/Module/HeadTest2")
set(intermediatefiles module/headtest1.h module/headtest2.h)
set(origfiles "${CMAKE_CURRENT_SOURCE_DIR}/headtest1.h"
              "${CMAKE_CURRENT_SOURCE_DIR}/headtest2.h")
file(REMOVE ${expfiles} ${intermediatefiles})
ecm_generate_headers(
    camelcase_headers
    HEADER_NAMES HeadTest1 HeadTest2
    PREFIX Module
)
if (NOT "${expfiles}" STREQUAL "${camelcase_headers}")
    message(FATAL_ERROR "camelcase_headers was set to \"${camelcase_headers}\" instead of \"${expfiles}\"")
endif()
check_files(GENERATED ${expfiles}
            ORIGINALS ${intermediatefiles})
check_files(GENERATED ${expfiles}
            ORIGINALS ${intermediatefiles})


###########################################################

message(STATUS "Test 5: REQUIRED_HEADERS")
set(camelcase_headers)
set(req_headers)
set(expfiles "${CMAKE_CURRENT_BINARY_DIR}/HeadTest1"
             "${CMAKE_CURRENT_BINARY_DIR}/HeadTest2")
set(origfiles headtest1.h headtest2.h)
set(origpaths "${CMAKE_CURRENT_SOURCE_DIR}/headtest1.h"
              "${CMAKE_CURRENT_SOURCE_DIR}/headtest2.h")
file(REMOVE ${expfiles})
ecm_generate_headers(
    camelcase_headers
    HEADER_NAMES HeadTest1 HeadTest2
    REQUIRED_HEADERS req_headers
)
if (NOT "${origpaths}" STREQUAL "${req_headers}")
    message(FATAL_ERROR "REQUIRED_HEADERS var was set to \"${req_headers}\" instead of \"${origpaths}\"")
endif()
if (NOT "${expfiles}" STREQUAL "${camelcase_headers}")
    message(FATAL_ERROR "camelcase_headers was set to \"${camelcase_headers}\" instead of \"${expfiles}\"")
endif()
check_files(GENERATED ${expfiles}
            ORIGINALS ${origfiles})


###########################################################

message(STATUS "Test 6: RELATIVE and REQUIRED_HEADERS")
set(camelcase_headers)
set(req_headers)
set(expfiles "${CMAKE_CURRENT_BINARY_DIR}/HeadTest2"
             "${CMAKE_CURRENT_BINARY_DIR}/HeadTest3")
set(origfiles headtest2.h headtest3.h)
set(origpaths "${CMAKE_CURRENT_SOURCE_DIR}/headsubdir/headtest2.h"
              "${CMAKE_CURRENT_SOURCE_DIR}/headsubdir/headtest3.h")
file(REMOVE ${expfiles})
ecm_generate_headers(
    camelcase_headers
    HEADER_NAMES HeadTest2 HeadTest3
    RELATIVE headsubdir
    REQUIRED_HEADERS req_headers
)
if (NOT "${origpaths}" STREQUAL "${req_headers}")
    message(FATAL_ERROR "REQUIRED_HEADERS var was set to \"${req_headers}\" instead of \"${origpaths}\"")
endif()
if (NOT "${expfiles}" STREQUAL "${camelcase_headers}")
    message(FATAL_ERROR "camelcase_headers was set to \"${camelcase_headers}\" instead of \"${expfiles}\"")
endif()
check_files(GENERATED ${expfiles}
            ORIGINALS ${origfiles})


###########################################################

message(STATUS "Test 7: OUTPUT_DIR and REQUIRED_HEADERS")
set(camelcase_headers)
set(req_headers)
set(expfiles "${CMAKE_CURRENT_BINARY_DIR}/testdir/HeadTest1"
             "${CMAKE_CURRENT_BINARY_DIR}/testdir/HeadTest2")
set(origfiles headtest1.h headtest2.h)
set(origpaths "${CMAKE_CURRENT_SOURCE_DIR}/headtest1.h"
              "${CMAKE_CURRENT_SOURCE_DIR}/headtest2.h")
file(REMOVE ${expfiles})
ecm_generate_headers(
    camelcase_headers
    HEADER_NAMES HeadTest1 HeadTest2
    OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/testdir"
    REQUIRED_HEADERS req_headers
)
if (NOT "${origpaths}" STREQUAL "${req_headers}")
    message(FATAL_ERROR "REQUIRED_HEADERS var was set to \"${req_headers}\" instead of \"${origpaths}\"")
endif()
if (NOT "${expfiles}" STREQUAL "${camelcase_headers}")
    message(FATAL_ERROR "camelcase_headers was set to \"${camelcase_headers}\" instead of \"${expfiles}\"")
endif()
check_files(GENERATED ${expfiles}
            ORIGINALS ${origfiles})


###########################################################

message(STATUS "Test 8: PREFIX and REQUIRED_HEADERS")
set(camelcase_headers)
set(req_headers)
set(expfiles "${CMAKE_CURRENT_BINARY_DIR}/Module/HeadTest1"
             "${CMAKE_CURRENT_BINARY_DIR}/Module/HeadTest2")
set(intermediatefiles module/headtest1.h module/headtest2.h)
set(origfiles "${CMAKE_CURRENT_SOURCE_DIR}/headtest1.h"
              "${CMAKE_CURRENT_SOURCE_DIR}/headtest2.h")
set(origpaths ${origfiles})
file(REMOVE ${expfiles} ${intermediatefiles})
ecm_generate_headers(
    camelcase_headers
    HEADER_NAMES HeadTest1 HeadTest2
    PREFIX Module
    REQUIRED_HEADERS req_headers
)
if (NOT "${origpaths}" STREQUAL "${req_headers}")
    message(FATAL_ERROR "REQUIRED_HEADERS var was set to \"${req_headers}\" instead of \"${origpaths}\"")
endif()
if (NOT "${expfiles}" STREQUAL "${camelcase_headers}")
    message(FATAL_ERROR "camelcase_headers was set to \"${camelcase_headers}\" instead of \"${expfiles}\"")
endif()
check_files(GENERATED ${expfiles}
            ORIGINALS ${intermediatefiles})
check_files(GENERATED ${expfiles}
            ORIGINALS ${intermediatefiles})


###########################################################

message(STATUS "Test 9: REQUIRED_HEADERS (duplicate var)")
set(all_headers)
set(expfiles "${CMAKE_CURRENT_BINARY_DIR}/HeadTest1"
             "${CMAKE_CURRENT_BINARY_DIR}/HeadTest2")
set(origfiles headtest1.h headtest2.h)
set(origpaths "${CMAKE_CURRENT_SOURCE_DIR}/headtest1.h"
              "${CMAKE_CURRENT_SOURCE_DIR}/headtest2.h")
file(REMOVE ${expfiles})
ecm_generate_headers(
    all_headers
    HEADER_NAMES HeadTest1 HeadTest2
    REQUIRED_HEADERS all_headers
)
list(SORT all_headers)
set(exp_headers ${expfiles} ${origpaths})
list(SORT exp_headers)
if (NOT "${exp_headers}" STREQUAL "${all_headers}")
    message(FATAL_ERROR "combined headers var was set to \"${all_headers}\" instead of \"${exp_headers}\"")
endif()
check_files(GENERATED ${expfiles}
            ORIGINALS ${origfiles})


###########################################################

message(STATUS "Test 10: ORIGINAL CAMELCASE")
set(forward_headers)
set(expfiles "${CMAKE_CURRENT_BINARY_DIR}/CamelCaseHeadTest1"
             "${CMAKE_CURRENT_BINARY_DIR}/CamelCaseHeadTest2")
set(origfiles CamelCaseHeadTest1.h CamelCaseHeadTest2.h)
file(REMOVE ${expfiles})
ecm_generate_headers(
    forward_headers
    ORIGINAL CAMELCASE
    HEADER_NAMES CamelCaseHeadTest1 CamelCaseHeadTest2
)
if (NOT "${expfiles}" STREQUAL "${forward_headers}")
    message(FATAL_ERROR "forward_headers was set to \"${forward_headers}\" instead of \"${expfiles}\"")
endif()
check_files(GENERATED ${expfiles}
            ORIGINALS ${origfiles})


###########################################################

message(STATUS "Test 11: PREFIX and ORIGINAL CAMELCASE")
set(forward_headers)
set(expfiles "${CMAKE_CURRENT_BINARY_DIR}/Module/CamelCaseHeadTest1"
             "${CMAKE_CURRENT_BINARY_DIR}/Module/CamelCaseHeadTest2")
set(intermediatefiles Module/CamelCaseHeadTest1.h Module/CamelCaseHeadTest2.h)
set(origfiles "${CMAKE_CURRENT_SOURCE_DIR}/headtest1.h"
              "${CMAKE_CURRENT_SOURCE_DIR}/headtest2.h")
file(REMOVE ${expfiles} ${intermediatefiles})
ecm_generate_headers(
    forward_headers
    ORIGINAL CAMELCASE
    HEADER_NAMES CamelCaseHeadTest1 CamelCaseHeadTest2
    PREFIX Module
)
if (NOT "${expfiles}" STREQUAL "${forward_headers}")
    message(FATAL_ERROR "forward_headers was set to \"${forward_headers}\" instead of \"${expfiles}\"")
endif()
check_files(GENERATED ${expfiles}
            ORIGINALS ${intermediatefiles})
check_files(GENERATED ${expfiles}
            ORIGINALS ${intermediatefiles})

###########################################################

message(STATUS "Test 12: COMMON_HEADER")

set(camelcase_headers)
set(expfiles "${CMAKE_CURRENT_BINARY_DIR}/HeadTest1"
             "${CMAKE_CURRENT_BINARY_DIR}/HeadTest2"
             "${CMAKE_CURRENT_BINARY_DIR}/HeadTest4"
             "${CMAKE_CURRENT_BINARY_DIR}/CommonHeader")
set(origfiles headtest1.h headtest2.h)
file(REMOVE ${expfiles})
ecm_generate_headers(
    camelcase_headers
    ORIGINAL LOWERCASE
    HEADER_NAMES HeadTest1 HeadTest2 HeadTest4
    COMMON_HEADER CommonHeader
)
if (NOT "${expfiles}" STREQUAL "${camelcase_headers}")
    message(FATAL_ERROR "camelcase_headers was set to \"${camelcase_headers}\" instead of \"${expfiles}\"")
endif()
check_files(GENERATED ${expfiles}
            ORIGINALS ${origfiles})

file(READ CommonHeader file_contents)
string(STRIP "${file_contents}" file_contents)
file(READ "${CMAKE_CURRENT_BINARY_DIR}/CommonHeader" exp_contents)
string(STRIP "${exp_contents}" exp_contents)
if (NOT "${file_contents}" STREQUAL "${exp_contents}")
    message(FATAL_ERROR "${generated_file} contains '${file_contents}' instead of '${exp_contents}'")
endif()


###########################################################

message(STATUS "Test 13: multiple classes and COMMON_HEADER")

set(camelcase_headers)
set(expfiles "${CMAKE_CURRENT_BINARY_DIR}/HeadTest1"
             "${CMAKE_CURRENT_BINARY_DIR}/HeadTest2"
             "${CMAKE_CURRENT_BINARY_DIR}/HeadTest2Add1"
             "${CMAKE_CURRENT_BINARY_DIR}/HeadTest2Add2"
             "${CMAKE_CURRENT_BINARY_DIR}/HeadTest4"
             "${CMAKE_CURRENT_BINARY_DIR}/CommonHeader")
set(origfiles headtest1.h headtest2.h headtest4.h)
file(REMOVE ${expfiles})
ecm_generate_headers(
    camelcase_headers
    ORIGINAL LOWERCASE
    HEADER_NAMES HeadTest1 HeadTest2,HeadTest2Add1,HeadTest2Add2 HeadTest4
    COMMON_HEADER CommonHeader
)
if (NOT "${expfiles}" STREQUAL "${camelcase_headers}")
    message(FATAL_ERROR "camelcase_headers was set to \"${camelcase_headers}\" instead of \"${expfiles}\"")
endif()
check_files(GENERATED ${expfiles}
            ORIGINALS ${origfiles})

file(READ CommonHeader file_contents)
string(STRIP "${file_contents}" file_contents)
file(READ "${CMAKE_CURRENT_BINARY_DIR}/CommonHeader" exp_contents)
string(STRIP "${exp_contents}" exp_contents)
if (NOT "${file_contents}" STREQUAL "${exp_contents}")
    message(FATAL_ERROR "${generated_file} contains '${file_contents}' instead of '${exp_contents}'")
endif()


###########################################################

message(STATUS "Test 14: HEADER_EXTENSION")
set(camelcase_headers)
set(expfiles "${CMAKE_CURRENT_BINARY_DIR}/HeadTest1"
             "${CMAKE_CURRENT_BINARY_DIR}/HeadTest2")
set(origfiles headtest1.hpp headtest2.hpp)
file(REMOVE ${expfiles})
ecm_generate_headers(
    camelcase_headers
    HEADER_NAMES HeadTest1 HeadTest2
    HEADER_EXTENSION hpp
)
if (NOT "${expfiles}" STREQUAL "${camelcase_headers}")
    message(FATAL_ERROR "camelcase_headers was set to \"${camelcase_headers}\" instead of \"${expfiles}\"")
endif()
check_files(GENERATED ${expfiles}
            ORIGINALS ${origfiles})


# vim:ft=cmake
